<?php

class Sys {

    private static $_instance;

    /**
     * 
     * @return Mongo_Mysql
     * 
     */
    private static function factory() {
        if (!self::$_instance) {
            self::$_instance = new Mongo_Mysql();
        }
        return self::$_instance;
    }
    /**
     * 
     * @param type $post
     * 
     */
    public static function addUser($post) {
        $birthday = mktime(0, 0, 0, $post["month"], $post["day"], $post["year"]);
        $password = md5($post["password"]);
        $arr = array(
            "fullname" => $post["fullname"],
            "password" => $password,
            "email" => $post["email"],
            "phone" => $post["phone"],
            "birthday" => $birthday,
            "gender" => $post["gender"],
            "joindate" => time(),
            "gold" => "0",
            "silver" => "0",
            "activecode" => $post["code"],
            "status" => "1"
        );
        self::factory()->insert("users", $arr);
    }
    /**
     * 
     * @param type $post
     * 
     */
    public static function updateUserCode($post) {
        $gold = $post["gold"] + 50;
        $arr = array(
            "fullname" => $post["fullname"],
            "password" => $post["password"],
            "email" => $post["email"],
            "phone" => $post["phone"],
            "birthday" => $post["birthday"],
            "gender" => $post["gender"],
            "joindate" => $post["joindate"],
            "logindate" => $post["logindate"],
            "gold" => $gold,
            "silver" => $post["silver"],
            "isBonus" => $post["isBonus"],
            "activecode" => $post["activecode"],
            "status" => $post["status"],
        );
        self::factory()->update("users", $arr, $post["_id"]);
    }
    /**
     * 
     * @param type $data
     * @param type $user_id
     * 
     */
    public static function addUserOdd($data, $user_id){
        $arr = array(
            "userid" => $user_id,
            "oddid" => $data["odd_id"],
            "match_id" => $data["match_id"],
            "money" => $data["money"],
            "value" => $data["index_key"],
            "submitdate" => time(),
            "status" => 0
        );
        self::factory()->insert("userodd", $arr);
    }
    
    public static function addUserOddSx($data, $user_id){
        $arr = array(
            "userid" => $user_id,
            "number" => $data["value"],
            "money" => $data["money"],
            "type" => $data["type"],
            "location" => $data["location"],
            "submitdate" => time(),
            "processdate" => '',
            "status" => 0
        );
        self::factory()->insert("useroddxs", $arr);
    }
    /**
     * 
     * @param type $data
     * 
     */
    public static function updateUserPass($data){
        $password = md5($data["newpass"]);
        $arr = array(
            "fullname" => $data["fullname"],
            "password" => $password,
            "email" => $data["email"],
            "phone" => $data["phone"],
            "birthday" => $data["birthday"],
            "gender" => $data["gender"],
            "joindate" => $data["joindate"],
            "gold" => $data["gold"],
            "silver" => $data["silver"],
            "isBonus" => $data["isBonus"],
            "logindate" => $data["logindate"],
            "activecode" => $data["activecode"],
            "status" => $data["status"]
        );
        self::factory()->update("users", $arr, $data["_id"]);
    }
    
    public static function updateUserInfo($data){
        $birthday = mktime(0, 0, 0, $data["month"], $data["day"], $data["year"]);
        $fullname = $data["fullname"];
        $gender = $data["gender"];
        $phone = $data["phone"];
        $arr = array(
            "fullname" => $fullname,
            "password" => $data["password"],
            "email" => $data["email"],
            "phone" => $phone,
            "birthday" => $birthday,
            "gender" => $gender,
            "joindate" => $data["joindate"],
            "gold" => $data["gold"],
            "silver" => $data["silver"],
            "isBonus" => $data["isBonus"],
            "logindate" => $data["logindate"],
            "activecode" => $data["activecode"],
            "status" => $data["status"]
        );
        self::factory()->update("users", $arr, $data["id"]);    
    }

    public static function updateUserMoney($data, $user_id){
        $money = $data["gold"] - $data["money"];
        $arr = array(
            "fullname" => $data["fullname"],
            "password" => $data["password"],
            "email" => $data["email"],
            "phone" => $data["phone"],
            "birthday" => $data["birthday"],
            "gender" => $data["gender"],
            "joindate" => $data["joindate"],
            "gold" => $money,
            "silver" => $data["silver"],
            "isBonus" => $data["isBonus"],
            "logindate" => $data["logindate"],
            "activecode" => $data["activecode"],
            "status" => $data["status"]
        );
        self::factory()->update("users", $arr, $user_id);    
    }
    
    public static function addPrize($data){
        $arr = array(
            "name" => $data["name"],
            "value" => $data["value"],
            "quantity" => $data["quantity"],
            "image" => $data["image"],
            "description" => $data["description"],
        );
        self::factory()->insert("prize", $arr);
    }
    
    public static function updatePrize($data){
        $quantity = $data["quantity"] - 1;
        $status = $quantity == 0 ? 0 : 1;
        $arr = array(
            "name" => $data["name"],
            "value" => $data["value"],
            "quantity" => $quantity,
            "image" => $data["image"],
            "description" => $data["description"],
            "status" => $status
        );
        self::factory()->update("prize", $arr, $data["_id"]);
    }
    
    public static function addUserPrize($user_id, $prize_id){
        $arr = array(
            "userid" => $user_id,
            "prizeid" => $prize_id,
            "requestdate" => date("Y-m-d"),
            "requestdateTS" => time(),
            "status" => 1,
        );
        self::factory()->delete("userprize", $arr);
    }
}

?>
